java.lang.IllegalArgumentException-Surface.lockCanvas-Surface was already locked

1
2
3
4
5
6
7
8
Exception locking surface
java.lang.IllegalArgumentException: Surface was already locked
at android.view.Surface.lockCanvas(Surface.java:273)
at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:977)
at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:945)
at com.caiyi.youle.camera.ui.VideoProgressView.myDraw(VideoProgressView.java:232)
at com.caiyi.youle.camera.ui.VideoProgressView.run(VideoProgressView.java:119)
at java.lang.Thread.run(Thread.java:818)

VideoProgressView.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class VideoProgressView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

@Override
public void surfaceCreated(SurfaceHolder holder) {
thread = new Thread(this);
drawing = true;
thread.start();
}

@Override
public void run() {
while (drawing) {
try {
myDraw();
Thread.sleep(80); //这里40毫秒更新一次.
} catch (Exception e) {
e.printStackTrace();
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
private void myDraw() {
canvas = holder.lockCanvas();

progressHeight = getMeasuredHeight();

if (canvas != null) {
canvas.drawRect(0, 0, screenWidth, progressHeight, backgroundPaint);
}

countWidth = 0;

if (!timeList.isEmpty()) {
long curSegmentTime = 0;

Iterator<Integer> iterator = timeList.iterator();
while (iterator.hasNext()) {

curSegmentTime = iterator.next(); //当前这一段的时间.

float left = countWidth;
countWidth += curSegmentTime * perWidth;
if (canvas != null) {
canvas.drawRect(left, 0, countWidth, progressHeight, progressPaint);
canvas.drawRect(countWidth, 0, countWidth + breakWidth, progressHeight, breakPaint);
}
countWidth += breakWidth;
}
}

if (timeList.isEmpty() || (!timeList.isEmpty() && timeList.getLast() <= minRecordTimeMS)) {
float left = perWidth * minRecordTimeMS;
if (canvas != null) {
canvas.drawRect(left, 0, left + minTimeWidth, progressHeight, minTimePaint);
}
}
if (currentState == State.BACKSPACE) {
float left = countWidth - timeList.getLast() * perWidth; //应该减去最后一段的时间.
float right = countWidth;
if (canvas != null) {
canvas.drawRect(left, 0, right, progressHeight, rollbackPaint);
}
}
/**
* 手指按下时,绘制新进度条
*
* 绘制一个新的刻度.
*
* 当设置新的时间过来后, 这里应该是两个时间戳的相减
*/
if (currentState == State.START) {
perProgress += perWidth * (curProgressTimeMs - lastProgressTimeMs);

float right = (countWidth + perProgress) >= screenWidth ? screenWidth : (countWidth + perProgress);

if (canvas != null) {
canvas.drawRect(countWidth, 0, right, progressHeight, progressPaint);
}
}

long curSystemTime = System.currentTimeMillis();
if (drawFlashTime == 0 || curSystemTime - drawFlashTime >= 500) {
isVisible = !isVisible;
drawFlashTime = curSystemTime;
}

if (isVisible) {
if (currentState == State.START) {
if (canvas != null) {
canvas.drawRect(countWidth + perProgress, 0, countWidth + flashWidth + perProgress, progressHeight,
flashPaint);
}
} else {
if (canvas != null) {
canvas.drawRect(countWidth, 0, countWidth + flashWidth, progressHeight, flashPaint);
}
}
}

lastProgressTimeMs = curProgressTimeMs;
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}